//
//  StockModel.m
//  TeamModel_1
//
//  Created by Ibokan on 15/11/16.
//  Copyright © 2015年 iicoffee. All rights reserved.
//

#import "StockModelView.h"
#import "StockInfoCell.h"
#import "StockModel.h"
#import "StockInfoModelViewController.h"
@interface StockModelView()

@end

@implementation StockModelView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        //布局代码
        
        //左上方不动的label:简称
        self.topLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, 0, 60, 40)];
        self.topLabel.text = @"简称";
        [self addSubview:self.topLabel];
        
        //左边tableview
        self.table1 = [[UITableView alloc]initWithFrame:CGRectMake(0, 40, 100, 520)];
        [self addSubview:self.table1];
        
        //scrollView的布局
        self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(100, 0, 280, 560)];
        [self addSubview:self.scrollView];
        
        //第一行的列表信息
        self.infoLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 800, 40)];
        self.infoLabel.text = @"       最新     |     涨幅    |    涨跌    |    换手%  |    成交量  |    成交额  |    量比    |    涨速%   |    振幅%    |    昨收  ";
        [self.scrollView addSubview:self.infoLabel];
        
        self.table2 = [[UITableView alloc]initWithFrame:CGRectMake(0, 40, 800, 520)];
        [self.scrollView addSubview:self.table2];
        
        self.table1.delegate = self;
        self.table1.dataSource = self;
        self.table2.delegate = self;
        self.table2.dataSource = self;
        
        //注册单元格
        [self.table1 registerClass:[UITableViewCell class] forCellReuseIdentifier:@"normal"];
        [self.table2 registerClass:[StockInfoCell class] forCellReuseIdentifier:@"stock"];
        
        
        //屏蔽滚动滑块
        self.table1.showsVerticalScrollIndicator = NO;
        self.table2.showsVerticalScrollIndicator = NO;
        self.scrollView.showsHorizontalScrollIndicator = NO;
        
        //设置scrollView属性
        self.scrollView.contentSize = self.table2.frame.size;
        self.scrollView.delegate = self;
        
        
        
    }
    return self;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.stockArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  
    
    StockModel *stockModel = self.stockArray[indexPath.row];
    
    if (tableView == self.table1) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"normal" forIndexPath:indexPath];
        if (stockModel != nil) {
            cell.textLabel.text = stockModel.stockName;
            cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:15.0f];
        }
        return cell;
    }
    else if (tableView == self.table2) {
        StockInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:@"stock" forIndexPath:indexPath];
        if (stockModel != nil) {
            //循环赋值给cell
            for (int i = 1; i < 11; i++) {
                UILabel * label = [cell viewWithTag:i];
                
                //设置字体大小
                UIFont *font = [UIFont fontWithName:@"Helvetica" size:13.5f];
                label.font = font;
                
                
                label.textAlignment = NSTextAlignmentCenter;
                
                switch (i) {
                    case 1:
                        label.text = [NSString stringWithFormat:@"    %@",stockModel.newest];
                        //判断是红色还是绿色
                        if ([stockModel.newest hasPrefix:@"-"]) {
                            label.textColor = [UIColor greenColor];
                        }else
                        {
                            label.textColor = [UIColor redColor];
                        }
                        break;
                    case 2:
                        label.text = [NSString stringWithFormat:@"    %@",stockModel.amountofIncrease];
                        label.textColor = [UIColor whiteColor];
                        //设置底色
                        if([stockModel.amountofIncrease hasPrefix:@"-"])
                        {
                            label.backgroundColor = [UIColor greenColor];
                        }else{
                            label.backgroundColor = [UIColor redColor];
                        }
                        
                        //修改label样式
                        label.textAlignment = NSTextAlignmentLeft;
                        label.frame = CGRectMake(110, 10, 70, 20);
                        
                        break;
                    case 3:
                        label.text = [NSString stringWithFormat:@"    %@",stockModel.upsAndDowns];
                        //判断是红色还是绿色
                        if ([stockModel.upsAndDowns hasPrefix:@"-"]) {
                            label.textColor = [UIColor greenColor];
                        }else
                        {
                            label.textColor = [UIColor redColor];
                        }
                        break;
                    case 4:
                        label.text = [NSString stringWithFormat:@"    %@",stockModel.handOFF];
                        break;
                    case 5:
                        label.text = [NSString stringWithFormat:@"    %@",stockModel.tradingVolume];
                        break;
                    case 6:
                        label.text = [NSString stringWithFormat:@"    %@",stockModel.tradingAccount];
                        break;
                    case 7:
                        label.text = [NSString stringWithFormat:@"    %@",stockModel.quantityRelativeRatio];
                        //判断是红色还是绿色
                        if ([stockModel.quantityRelativeRatio hasPrefix:@"-"]) {
                            label.textColor = [UIColor greenColor];
                        }else
                        {
                            label.textColor = [UIColor redColor];
                        }
                        break;
                    case 8:
                        label.text = [NSString stringWithFormat:@"    %@",stockModel.UpSpeed];
                        //判断是红色还是绿色
                        if ([stockModel.UpSpeed hasPrefix:@"-"]) {
                            label.textColor = [UIColor greenColor];
                        }else
                        {
                            label.textColor = [UIColor redColor];
                        }
                        break;
                    case 9:
                        label.text = [NSString stringWithFormat:@"    %@",stockModel.swing];
                        break;
                    case 10:
                        label.text = [NSString stringWithFormat:@"    %@",stockModel.yesterAccount];
                        break;
                    default:
                        break;
                }
            }
            
        }
        return cell;
    }else
    {
        return nil;
    }
    
}

//联动方法
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGPoint offset = CGPointMake(0, scrollView.contentOffset.y);
    
    if (scrollView == self.table1)
    {
        if (fabs(scrollView.contentOffset.y) > 0)
        {
            [self.table2 setContentOffset:offset];
        }
    }
    else
    {
        if (fabs(scrollView.contentOffset.y) > 0)
        {
            [self.table1 setContentOffset:offset];
        }
    }
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString* name = [self.stockArray[indexPath.row] stockName];
    NSLog(@"%@",name);
    [[NSNotificationCenter defaultCenter] postNotificationName:@"transfrom" object:self];
}


@end
